<?php

namespace Common\Service;

//发送邮件
class Email
{
    public static function sendEmail($to, $title, $content)
    {
        set_time_limit(0);
        $config = C('email');
        vendor('PHPMailer.src.SMTP');
        vendor('PHPMailer.src.PHPMailer');
        $mail = new \PHPMailer(); //实例化
        $mail->IsSMTP(); // 启用SMTP
        $mail->Host            = $config['MAIL_HOST']; //smtp服务器的名称（这里以QQ邮箱为例）
        $mail->SMTPAuth        = $config['MAIL_SMTPAUTH']; //启用smtp认证
        $mail->Username        = $config['MAIL_USERNAME']; //你的邮箱名
        $mail->Password        = $config['MAIL_PASSWORD']; //邮箱密码
        $mail->Port            = $config['MAIL_PORT']; // SMTP服务器的端口号
        $mail->From            = $config['MAIL_FROM']; //发件人地址（也就是你的邮箱地址）
        $mail->FromName        = $config['MAIL_FROMNAME']; //发件人姓名
        $mail->MAIL_SMTPSECURE = $config['MAIL_SMTPSECURE']; // 使用安全协议
        $mail->IsHTML          = $config['MAIL_ISHTML']; // 是否HTML格式邮件
        $mail->CharSet         = $config['MAIL_CHARSET']; //设置邮件编码

        $mail->AddAddress($to, "尊敬的客户");
        $mail->SMTPDebug = 0; // 关闭SMTP调试功能
        // 1 = errors and messages
        // 2 = messages only
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->Subject  = $title; //邮件主题
        $mail->Body     = $content; //邮件内容
        $mail->AltBody  = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
        //设置回复人的邮件名称地址
        $replyEmail = $config['MAIL_REPLY'] ? $config['MAIL_REPLY'] : $config['MAIL_FROM'];
        $replyName  = $config['MAIL_REPLY_NAME'] ? $config['MAIL_REPLY_NAME'] : $config['MAIL_USERNAME'];
        $mail->AddReplyTo($replyEmail, $replyName);

        /*
        // 添加附件
        if(is_array($attachment)){
        foreach ($attachment as $file){
        is_file($file) && $mail->AddAttachment($file);
        }
        }
         */
        return $mail->Send() ? true : $mail->ErrorInfo;
    }

    /**
     * 获取发送邮件的html
     * @param  [type] $id [任务id]
     * @return [type]     [description]
     */
    public static function sendEmailContent($id, $no, $name, $create_time, $end_time, $uname, $followName, $remark)
    {
        $content = '<!DOCTYPE html><html><head><title>email</title><script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="js/size.js"></script>
        <style type="text/css">
            .title{margin-top: 50px;margin-bottom: 50px;margin-left: 20px;font-weight: bold;font-size: 16px;}
            .info{margin-left: 20px;margin-top: 10px;}
            .info .t{float: left;color:#999999;}
            .info .c{margin-left: 20%;}
        </style>
        </head><body><div>
            <div class="title"><p>任务提醒</p></div>
            <div class="info"><div class="t">任务编码</div><div class="c">' . $no . '</div></div>
            <div class="info"><div class="t">任务名称</div><div class="c">' . $name . '</div></div>
            <div class="info"><div class="t">下单时间</div><div class="c">' . $create_time . '</div></div>
            <div class="info"><div class="t">截止时间</div><div class="c">' . $end_time . '</div></div>
            <div class="info"><div class="t">下&nbsp;&nbsp;单&nbsp;&nbsp;人</div><div class="c">' . $uname . '</div></div>
            <div class="info"><div class="t">跟&nbsp;&nbsp;进&nbsp;&nbsp;人</div><div class="c">' . $followName . '</div></div>
            <div class="info"><div class="t">温馨提示</div><divclass="c">' . $remark . '</div></div></br>
            <div class="info"><div class="t"></div><div class="c"><a href="http://rc.xiutub.com/Home/Index/editTask?id=' . $id . '">查看详情</a></div></div>
        </div></body></html>';

        return $content;
    }
}
